package com.activenetwork.smlite.testcase.events.eventListSection;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.activenetwork.smlite.testcase.BaseCase;
import com.activenetwork.util.EnvContext;
import com.activenetwork.util.grid.OperatableGrid;
import com.activenetwork.util.grid.gson.GridFactory;
import com.activenetwork.util.verification.gson.FailurePoint;

public class EventListSection009 extends BaseCase {
	static final Logger logger = LoggerFactory.getLogger(EventListSection009.class);
	private String userName;
	private String userPwd;
	private OperatableGrid grid;
	
	@BeforeClass(alwaysRun = true)
	public void dataSetup(){
		userName = "smlite@starcite.com";
		userPwd = "st@rc!te";
	}
	
	@Test
	public void Verify_pagination_of_Event_List_data_grid_works_correctly(){
		loginBusiness.login(userName, userPwd);
		
		// the grid on the home view page
		grid = GridFactory.getGrid("EventsHomeView", "SearchEventGrid");
		
		// Go through each page and remember the event of 1st row
		By eventLink = By.cssSelector(".main-content.autotest-bamboo-eventshomeview table tbody[style=''] tr:first-child a");
		List<String> eventNames = new ArrayList<String>();
		while(true){
			WebElement a = webDriver.findElement(eventLink);
			eventNames.add(a.getText().trim());
			
			if(grid.isLastPage()){
				break;
			}else{
				grid.nextPage();
			}
		}
		
		// Go back to 1st page
		grid.firstPage();
		
		// Verify 1st page  1st row event name
		String checkpoint1name = webDriver.findElement(eventLink).getText().trim();
		if(!StringUtils.equals(eventNames.get(0), checkpoint1name)){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection009.class.getName(), 
					"Verify pagination by page number", 
					"Pagination by page number failed"));
		}
		
		// Verify the next page, go to last page check 1st row event name
		for(int i=1;i<eventNames.size();i++){
			grid.nextPage();
		}
		String checkpoint2name = webDriver.findElement(eventLink).getText().trim();
		if(!StringUtils.equals(eventNames.get(eventNames.size()-1), checkpoint2name)){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection009.class.getName(), 
					"Verify pagination by next page", 
					"Pagination by next page failed"));
		}
		
		// Verify the previous page, to to 1st page check 1st row event name
		while(!grid.isFirstPage()){
			grid.previousPage();
		}
		String checkpoint3name = webDriver.findElement(eventLink).getText().trim();
		if(!StringUtils.equals(eventNames.get(0), checkpoint3name)){
			EnvContext.failurePointTL.get().add(
					new FailurePoint(EventListSection009.class.getName(), 
					"Verify pagination by previous page", 
					"Pagination by previous page failed"));
		}
		
		loginBusiness.logout();
		
	}
}
